<?php

class Profile extends CI_Controller{

	
	function __construct(){
		parent::__construct();
	}
	
	
	/**function for viewing profiles
	 * @param account: the name of the profile to view
	 */
	function show($account) {

		$this->load->model('profile_model');
		$this->load->library('table');
		$this->load->helper('date');

		$params['profile_model'] = &$this->profile_model;
		$params['table_library'] = &$this->table;
		
		//Get general user info
		$user_data = $this->profile_model->get_data($account);
		
		//If this is the logged in user's account : get challenges
		if($account == $this->session->userdata('account'))
			$user_data['challenges'] = $this->challenge_object->load_user_challenge($account);
		
		//Add ladder rank
		$this->load->model('ladder_model');
		$this->ladder_model->refresh_table();
		$user_data['rank'] = $this->ladder_model->get_Rank($account);
					
		$params['subview'] = 'subview/profile_view';

		//If the person exists - set data and attempt to fetch bnet info 
		if($user_data) {
			
			$params['user_data'] = $user_data;
			//Only attempt to fetch info if bnet url is supplied
//			if(strlen($user_data['bnet']) > 0) {
//				$this->load->helper('webparser');
//				
//				//BROKEN - USING TEMPORARY REPLACEMENT
//				$ranksLink = get_ranks_url($user_data['bnet']);
//				if($ranksLink === false){
//					$params['bnetinfo'] = 'page Down';
//					
//				} else 
//					$params['bnetinfo'] = get_sc_ranks_info($ranksLink);
//					$params['picture'] = get_picture($ranksLink);
//				//$params['bnetinfo'] = 'Battle.net info goes here.'; //TEMPORARY
//			}
		}

		//load view
		$this->load->view('homepage_view', $params);

	}
	

	
	/**
	 * This function loads the profile editing page
	 * */
	function edit($error='') {
		//prepare parameters
		$params['subview'] = 'subview/profile_edit_view';
		$params['user_data'] = $this->session->userdata('profile_data');
		if($error !='') $params['db_error'] = $error;
		//load view
		$this->load->view('homepage_view', $params);

	}
	
	/**
	 * performs form validation for profile modification
	 * if valid it updates the database and redirects to the profile page
	 * otherwise it reloads teh page with error messages
	 * */
	function save_changes() {
		//for convenience
		$account=$this->session->userdata('account');
		
		$this->load->library('form_validation');

		//Enforce rules of form fields.
		$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|alpha');
		$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|alpha');
		$this->form_validation->set_rules('newpassword', 'New Password', 'trim|min_length[4]');
		$this->form_validation->set_rules('conf_newpassword', 'New Password', 'trim|min_length[4]|matches[newpassword]');
		$this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email');
		$this->form_validation->set_rules('bnet', 'Battle.net URL', 'trim');
		$this->form_validation->set_rules('race', 'Favourite Race', 'trim');
		$this->form_validation->set_rules('birthday', 'Birthday yyyy-mm-dd', 'trim|alpha_dash');

		//only require password when setting a new password
		if(strlen($this->input->post('newpassword')) > 3)
			$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]');
		
		if($this->form_validation->run()){
			//validation successful
				
			//save data from form
			$input_data = array(
				'first_name' => $this->input->post('first_name', true),
				'last_name'	 => $this->input->post('last_name', true),
				'password'	=> $this->input->post('password', true),
				'newpassword' => $this->input->post('newpassword', true),
				'birthday' => $this->input->post('birthday', true),
				'bnet' => $this->input->post('bnet'),
				'race' => $this->input->post('race'),
				'email' => $this->input->post('email')
			);
			//load profile model
			$this->load->model('profile_model');
			//save to db
			$query = $this->profile_model->update_profile($account, $input_data);
			
			if($query){
				//update session variable for profile data
				$this->session->set_userdata(array('profile_data' => $this->profile_model->get_data($account)));
				//echo $this->session->userdata('account');
				//back to profile page
				redirect('/profile/show/'.$account);
				
			}
			else {
				$this->edit('Password incorrect.');
			}
		}
		else {
			$this->edit();
		}
	}


}
